package com.Telien.app.user.service;

import java.util.ArrayList;
import java.util.List;

import com.Teilen.app.db.main.DBWrapper;
import com.Telien.app.request.object.DriverRequest;
import com.Telien.app.request.object.rideService;

public class SessionManagementService {
	ArrayList<rideService> sysSessions = new ArrayList<rideService>();
	DBWrapper dbWrapper;
	
	public SessionManagementService() {
		populateSessions();		
	}
	
	public String getRideServiceActivity(int sId) {
		for (rideService rs : sysSessions) {
			if (rs.getrideSessionID() == sId) {
				if (rs.getactivity())
					return "Accepted";
				else
					return "Not Accepted";
			}
		}
		return "Unknown";
	}
	
	public void printSessionIds() {
		if (sysSessions.isEmpty()) {
			System.out.println("Empty");
			return;
		}
			
		for (rideService rs : sysSessions) {
			System.out.println(rs.getrideSessionID());
		}
	}
	
	public int getSize() {
		return dbWrapper.selectSessionsSize();
	}
	
	public ArrayList<rideService> getSessions() {
		return sysSessions;
	}
	
	public ArrayList<rideService> getSessionsWhereDriverId(int id) {
		ArrayList<rideService> sessions = new ArrayList<rideService>();
		for (rideService rs : sysSessions) {
			if (rs.getdriverRequestUserID() == id) {
				sessions.add(rs);
			}
		}
		return sessions;
	}
	
	public ArrayList<rideService> getUsersSessions(int uId) {
		ArrayList<rideService> sessions = new ArrayList<rideService>();
		for (rideService rs : sysSessions) {
			if (rs.getrequestUserID() == uId) {
				sessions.add(rs);
			}
		}
		return sessions;
	}
	
	public ArrayList<DriverRequest> getRequestsAtSessionId(int sId) {
		ArrayList<DriverRequest> requests = new ArrayList<DriverRequest>();
		for (rideService rs : sysSessions) {
			if (rs.getrideSessionID() == sId) {
				requests.add(rs.getRequest());
			}
		}
		return requests;
	}
	
	
	// Using this once for testing. DO NOT CALL THIS EVER
	public void deleteAllSessions() {
		dbWrapper = new DBWrapper();
		dbWrapper.deleteAllSessions();
		this.populateSessions();
	}

	private void populateSessions() {
		dbWrapper = new DBWrapper();
		List<rideService> sessions = dbWrapper.getSessions();
		for (rideService session : sessions) 
			sysSessions.add(session);
	}
}
